{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "summary\n",
      "This work is an argument about the attitude towards cloning. The appearance of Dolly caused a complicated discussion about cloning. Some powerful people were against the human cloning and make many efforts to stop the research about it. There are some people think that cloning is a danger even a threat to human being, which will disobey the will of God. However, the writer suggests that we shouldn’t stop the research about cloning.\n",
      "The writer thinks that just like the “test-tube babies”, the cloning has a promising future which will make parents feel happier. What’s more, even the people who were cloned by nature, they are still not same. Therefore, cloning doesn’t make individuals’ character disappear. The writer says the parents may know the children better, and it may lead to a better family relationship. It is unnessasary to worry about cloned warriors because the cloning will grow form a baby which has no threat to human being. Also, it won’t disobey the God’s will for God creates Eve just by method like cloning. All in all, we shouldn’t stop the research about cloning because of our bad imagination and fear.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "import docx\n",
    " #新建文档\n",
    "#doc_new = docx.Document()\n",
    " \n",
    "#读取文档\n",
    "doc = docx.Document(r'D:\\360极速浏览器下载\\luningyingyu\\summary.docx')\n",
    "#doc.save('D:/360极速浏览器下载\\luningyingyu\\summary.docx')\n",
    "for para in doc.paragraphs:\n",
    "  print(para.text)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "r'D:\\360极速浏览器下载\\luningyingyu\\summary.docx'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [],
   "source": [
    "# coding=utf-8\n",
    "\n",
    "from docx import Document  # 文档对象\n",
    "from docx.shared import Inches # 英尺\n",
    "\n",
    "# 创建文档对象\n",
    "document = Document()\n",
    "\n",
    "# 增加文档标题（0级标题）\n",
    "document.add_heading('文章标题',0)\n",
    "\n",
    "# 增加段落\n",
    "p = document.add_paragraph('你让他用戒指把你套上的时候，我察觉到你脸上复杂的笑容。')\n",
    "\n",
    "# 段落后追加内容并设置追加的内容为粗体\n",
    "p.add_run('追加的段落内容，样式为粗体。').bold = True\n",
    "\n",
    "# 段落后追加内容并设置追加的内容为斜体\n",
    "p.add_run('追加的段落内容，样式为斜体').italic = True\n",
    "\n",
    "# 添加一级标题\n",
    "document.add_heading('一级标题', level = 1)\n",
    "\n",
    "#添加段落并设置段落为引用样式\n",
    "document.add_paragraph('添加段落并添加了样式，此处的样式为引用', style= 'Intense Quote')\n",
    "\n",
    "#增加段落并在段落前面加上项目符号（List Bullet样式为）\n",
    "document.add_paragraph(\n",
    "    '此处是项目符号样式', style = 'List Bullet'\n",
    ")\n",
    "\n",
    "#增加段落并添加编号\n",
    "document.add_paragraph(\n",
    "    '此处是编号样式', style = 'List Number'\n",
    ")\n",
    "\n",
    "document.save('do_test.docx')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [],
   "source": [
    "# coding=utf-8\n",
    "\n",
    "from docx import Document  # 文档对象\n",
    "from docx.enum.text import WD_ALIGN_PARAGRAPH\n",
    "\n",
    "document = Document()\n",
    "\n",
    "# 定义三个段落，分别演示左中右对齐\n",
    "paragraph1 = document.add_paragraph(\n",
    "    '段落对齐测试（水平左对齐）'\n",
    ")\n",
    "\n",
    "paragraph2= document.add_paragraph(\n",
    "    '段落对齐测试（水平居中对齐）'\n",
    ")\n",
    "\n",
    "paragraph3= document.add_paragraph(\n",
    "    '段落对齐测试（水平右对齐）'\n",
    ")\n",
    "# 定义段落样式\n",
    "paragraph_format1 = paragraph1.paragraph_format\n",
    "paragraph_format2 = paragraph2.paragraph_format\n",
    "paragraph_format3 = paragraph3.paragraph_format\n",
    "\n",
    "# 在段落样式上添加对齐属性，可以添加其他更多属性\n",
    "paragraph_format1.alignment = WD_ALIGN_PARAGRAPH.LEFT\n",
    "paragraph_format2.alignment = WD_ALIGN_PARAGRAPH.CENTER\n",
    "paragraph_format3.alignment = WD_ALIGN_PARAGRAPH.RIGHT\n",
    "\n",
    "document.save('test_1.docx')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [],
   "source": [
    "from docx.shared import Pt #像素\n",
    "\n",
    "document = Document()\n",
    "\n",
    "#定义三个段落，分别演示左中右对齐\n",
    "paragraph1 = document.add_paragraph(\n",
    "   '段落缩进（左缩进）'\n",
    ")\n",
    "\n",
    "paragraph2= document.add_paragraph(\n",
    "   '段落缩进（右缩进）'\n",
    ")\n",
    "\n",
    "paragraph3= document.add_paragraph(\n",
    "   '首行缩进首行缩进首行缩进首行缩进首行缩进首行缩进首行缩进首行缩进首行缩进首行缩进首行缩进首行缩进首行缩进首行缩进首行缩进'\n",
    ")\n",
    "\n",
    "#定义段落样式\n",
    "paragraph_format1 = paragraph1.paragraph_format\n",
    "paragraph_format2 = paragraph2.paragraph_format\n",
    "paragraph_format3 = paragraph3.paragraph_format\n",
    "\n",
    "\n",
    "#左缩进0.5英尺\n",
    "paragraph_format1.left_indent = Inches(0.5)\n",
    "\n",
    "#右缩进24像素\n",
    "paragraph_format2.right_indent = Pt(24)\n",
    "\n",
    "#首行缩进（负值表示悬挂缩进）\n",
    "#paragraph_format3.first_line_indent = Inches(0.5)\n",
    "paragraph_format3.first_line_indent = Inches(-0.5)\n",
    "\n",
    "document.save('test_4.docx')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [],
   "source": [
    "from docx.shared import Pt #像素\n",
    "\n",
    "document = Document()\n",
    "paragraph1 = document.add_paragraph(\n",
    "   'jnfdnvc vkhjrvfd vnmcxfhdgfbdh cnxfbhsd c'\n",
    ")\n",
    "\n",
    "paragraph2= document.add_paragraph(\n",
    "   'cdsbfbbdgdsdvw3yurujfdfnncm'\n",
    ")\n",
    "\n",
    "paragraph3= document.add_paragraph(\n",
    "   '''vnfuncxckskdoejfurehvjnvjc\n",
    "   dcdshbkhcuohcndj\n",
    "   dncsuehudstygcvc'''\n",
    ")\n",
    "paragraph_format1 = paragraph1.paragraph_format\n",
    "paragraph_format2 = paragraph2.paragraph_format\n",
    "paragraph_format3 = paragraph3.paragraph_format\n",
    "#段前间距\n",
    "paragraph_format2.space_before = Pt(18)\n",
    "#段后间距\n",
    "paragraph_format2.space_after = Pt(12)\n",
    "from docx.shared import Length\n",
    "paragraph_format3.line_spacing = Pt(18)\n",
    "document.save('test_6.docx')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [],
   "source": [
    "from docx.shared import RGBColor\n",
    "document = Document()\n",
    "run = document.add_paragraph(\"我是最棒的\").add_run(\"我正在努力，我已经是很好的了，我在成为自己想成为的自己\")#add——run追加段落。\n",
    "font = run.font\n",
    "#设置字体字号斜体粗体字体颜色\n",
    "font.name = 'Calibri'\n",
    "font.size = Pt(12)\n",
    "font.italic = True\n",
    "font.bold = True\n",
    "font.color.rgb = RGBColor(0x42, 0x24, 0xE9)\n",
    "document.save(\"text_7.docx\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 添加图片并设置宽度为5英寸\n",
    "from docx import Document  # 文档对象\n",
    "from docx.shared import Inches\n",
    "document.add_picture('D:\\james jiaonang\\微信图片_20211113111055.jpg', width=Inches(5))\n",
    "#创建一个二维元组，保存即将操作的表格的内容\n",
    "records = (\n",
    "    (1, '张三', '30'),\n",
    "    (2, '李四', '35'),\n",
    "    (3, '王五', '38')\n",
    ")\n",
    "\n",
    "# 创建一个1行3列的表格\n",
    "table = document.add_table(rows=1,cols=3)\n",
    "\n",
    "# 在表格第一行填充内容\n",
    "hdr_cells = table.rows[0].cells\n",
    "hdr_cells[0].text= '编号'\n",
    "hdr_cells[1].text= '姓名'\n",
    "hdr_cells[2].text= '年龄'\n",
    "\n",
    "# 将定义好的表格内容追加进表格中\n",
    "for bh, nl, xb in records:\n",
    "    # 表格追加一行并分别填充1，2，3单元格\n",
    "    row_cells = table .add_row().cells\n",
    "    row_cells[0].text = str(bh)\n",
    "    row_cells[1].text = nl\n",
    "    row_cells[2].text = xb\n",
    "\n",
    "# 添加分页符\n",
    "document.add_page_break()\n",
    "\n",
    "#保存文档\n",
    "document.save('text_9.docx')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "#找自己电脑上的.doc后缀的word文档，调研相关的库和依赖，进行读写处理。\n",
    "\n",
    "from win32com import client as wc\n",
    "\n",
    "word = wc.Dispatch(\"Word.Application\")\n",
    "doc = word.Documents.Open(\"D:\\电子知识库\\电子书\\语言＋作业说明.doc\")\n",
    "doc.SaveAs(\"yuyanjia2.docx\", 12)  # 12为docx\n",
    "doc.Close()\n",
    "word.Quit()\n",
    "import docx\n",
    "docStr = Document(\"C:\\Users\\未来可期\\Documents\\yuyanjia2.docx\")   打开文档\n",
    "for paragraph in docStr.paragraphs:\n",
    "    parStr = paragraph.text"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Collecting pypiwin32\n",
      "  Downloading pypiwin32-223-py3-none-any.whl (1.7 kB)\n",
      "Requirement already satisfied: pywin32>=223 in d:\\2345downloads\\lib\\site-packages (from pypiwin32) (228)\n",
      "Installing collected packages: pypiwin32\n",
      "Successfully installed pypiwin32-223\n",
      "Note: you may need to restart the kernel to use updated packages.\n"
     ]
    }
   ],
   "source": [
    "pip install pypiwin32"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "在首位\n",
      "倒数第二\n",
      "员工表（新）\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "D:\\temp/ipykernel_1684/3281282301.py:31: DeprecationWarning: Call to deprecated function remove_sheet (Use wb.remove(worksheet) or del wb[sheetname]).\n",
      "  book.remove_sheet(sheet)\n"
     ]
    }
   ],
   "source": [
    "from openpyxl import Workbook  # 引入工作薄\n",
    "import datetime\n",
    "\n",
    "#创建工作薄\n",
    "book = Workbook()\n",
    "#选中活动表（默认第一张表，即Sheet1）\n",
    "sheet = book.active\n",
    "\n",
    "#A1单元格设置为42\n",
    "sheet['A1'] = 42\n",
    "#追加数据，分别填入单元格A2,B2,C2\n",
    "sheet.append([1, 2, 3])\n",
    "\n",
    "#A3单元格设置当前时间\n",
    "sheet['A3'] = datetime.datetime.now()\n",
    "\n",
    "# 另一种定位单元格的方法（第四行第二列即B4设置为16）\n",
    "sheet.cell(row=4, column=2).value = 16\n",
    "\n",
    "#创建新工作表 员工表(在工作表列表的尾部创建)\n",
    "emp = book.create_sheet('员工表')\n",
    "#创建新的工作表（在工作表列表的首位创建）\n",
    "first = book.create_sheet('在首位',0)\n",
    "# 在倒数第二的位置创建新的工作表\n",
    "pen = book.create_sheet('倒数第二',-1)\n",
    "#工作表重命名\n",
    "emp.title='员工表（新）'\n",
    "#工作表标签设置背景色\n",
    "first.sheet_properties.tabColor = \"1072BA\"\n",
    "#删除工作表\n",
    "book.remove_sheet(sheet)\n",
    "book.save(\"ex3.xlsx\")\n",
    "#遍历工作表\n",
    "for sheet in book:\n",
    "    print(sheet.title)\n",
    "\n",
    "#保存工作薄\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "42\n",
      "None\n",
      "1\n",
      "2\n",
      "2022-05-15 18:46:09.628000\n",
      "None\n",
      "None\n",
      "16\n",
      "None\n",
      "2\n",
      "None\n",
      "16\n",
      "42\n",
      "None\n",
      "None\n",
      "None\n",
      "1\n",
      "2\n",
      "3\n",
      "None\n",
      "2022-05-15 18:46:09.628000\n",
      "None\n",
      "None\n",
      "None\n",
      "None\n",
      "16\n",
      "None\n",
      "None\n"
     ]
    }
   ],
   "source": [
    "import openpyxl\n",
    "#打开文档\n",
    "book = openpyxl.load_workbook('ex2.xlsx')\n",
    "#选择工作表(此处选择之前有值的一个工作表)\n",
    "sheet = book['Sheet']\n",
    "#获取单元格A1的值\n",
    "c = sheet['A1']\n",
    "#print(c.value)\n",
    "#使用切片访问单元格范围(返回值是二维元组)\n",
    "cell_range = sheet['A1':'B4']\n",
    "for row in cell_range:\n",
    "    for cell in row:\n",
    "        print(cell.value)\n",
    "        #获取一列数据\n",
    "colB = sheet['B']\n",
    "for cell in colB:\n",
    "    print(cell.value)\n",
    "    #获取两列数据\n",
    "col_range = sheet['C:D']\n",
    "#获取一行数据\n",
    "row2 = sheet[2]\n",
    "#获取多行数据\n",
    "row_range = sheet[1:2]\n",
    "\n",
    "#获取整个工作表的值\n",
    "for row in sheet.values:\n",
    "   for value in row:\n",
    "     print(value)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "import openpyxl\n",
    "book = openpyxl.load_workbook('ex2.xlsx')\n",
    "sheet = book['Sheet']\n",
    "#合并单元格，仅保留合并区域左上角单元格的值\n",
    "sheet.merge_cells('A1:B1')\n",
    "#取消合并单元格\n",
    "#sheet.unmerge_cells('A1:B1')\n",
    "\n",
    "#同样可采用制定行列的方式来制定合并区域\n",
    "sheet.merge_cells(start_row=2, start_column=1, end_row=4, end_column=4)\n",
    "#折叠AB两列\n",
    "sheet.column_dimensions.group('A','B',hidden=True)\n",
    "#折叠1，2两行\n",
    "sheet.row_dimensions.group(1,2,hidden = True)\n",
    "#img = Image('D:\\james jiaonang\\微信图片_20211113111055.jpg')\n",
    "#sheet.add_image(img, 'D5')\n",
    "book.save(\"ex6.xlsx\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Requirement already satisfied: xlwt in d:\\2345downloads\\lib\\site-packages (1.3.0)\n",
      "Note: you may need to restart the kernel to use updated packages.\n"
     ]
    }
   ],
   "source": [
    "pip install xlwt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Requirement already satisfied: xlrd in d:\\2345downloads\\lib\\site-packages (2.0.1)\n",
      "Note: you may need to restart the kernel to use updated packages.\n"
     ]
    }
   ],
   "source": [
    "pip install xlrd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "import xlrd,xlwt\n",
    "xls_fileA = xlrd.open_workbook(r\"C:\\Users\\未来可期\\Documents\\temp\\2022.5.3作息时间表（有调整）.xls\")\n",
    "\n",
    "xls_sheetA = xls_fileA.sheets()[ 0 ] \n",
    "num_A = xls_sheetA.nrows \n",
    "row = xls_sheetA.row_values(0)\n",
    "newFile = xlwt.Workbook()\n",
    "\n",
    "newFile_sheet = newFile.add_sheet('sheet1')\n",
    "\n",
    "newFile_sheet.write(0,1,88)#//写文件，0第几行，newFile_row_one要写入的数据\n",
    "\n",
    "#newFile.save(\"D:\\电子知识库\\Vescode\\python2.0\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Requirement already satisfied: pywin32 in d:\\2345downloads\\lib\\site-packages (228)\n",
      "Note: you may need to restart the kernel to use updated packages.\n"
     ]
    }
   ],
   "source": [
    "pip install pywin32"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "#课后作业\n",
    "#准备若干类别Office文档，doc\\docx\\xls\\xlsx等都要有\n",
    "#学习并实验通过Win32 API调用MS Office应用程序和WPS Office应用程序后台，操作Office文档。\n",
    "import win32com\n",
    "from win32com.client import Dispatch, constants\n",
    "  \n",
    "w = win32com.client.Dispatch('Word.Application')\n",
    "# 或者使用下面的方法，使用启动独立的进程：\n",
    "# w = win32com.client.DispatchEx(\\'Word.Application\\')\n",
    "  \n",
    "# 后台运行，不显示，不警告\n",
    "w.Visible = 0\n",
    "w.DisplayAlerts = 0\n",
    "  \n",
    "# 打开新的文件\n",
    "doc = w.Documents.Open( r\"D:\\未来可期\\2022春季学期语言+课程期中期末作业说明黑\"  )\n",
    "# worddoc = w.Documents.Add() # 创建新的文档\n",
    "\n",
    "# 读取内容\n",
    "#data = doc.paragraphs[2].text\n",
    "  \n",
    "# 插入文字\n",
    "'''myRange = doc.Range(0,0)\n",
    "myRange.InsertBefore('Hello from Python!')\n",
    "myRange.InsertAfter('Hack word in python')\n",
    "# 使用样式\n",
    "wordSel = myRange.Select()\n",
    "wordSel.Style = constants.wdStyleHeading1\n",
    "  \n",
    "# 正文文字替换\n",
    "Oldstr=\"最终作业提交时间：结课后一周时间内\"\n",
    "Newstr=\"8879074382930457\"\n",
    "\n",
    "w.Selection.Find.ClearFormatting()\n",
    "w.Selection.Find.Replacement.ClearFormatting()\n",
    "w.Selection.Find.Execute(Oldstr, False, False, False, False, False, True, 1, True, Newstr, 2)\n",
    "  \n",
    "# 页眉文字替换\n",
    "w.ActiveDocument.Sections[0].Headers[0].Range.Find.ClearFormatting()\n",
    "w.ActiveDocument.Sections[0].Headers[0].Range.Find.Replacement.ClearFormatting()\n",
    "w.ActiveDocument.Sections[0].Headers[0].Range.Find.Execute(Oldstr, False, False, False, False, False, True, 1, False, Newstr, 2)'''\"news sharing2.0.docx\"\n",
    "# 转换为html\n",
    "wc = win32com.client.constants\n",
    "w.ActiveDocument.WebOptions.RelyOnCSS = 1\n",
    "w.ActiveDocument.WebOptions.OptimizeForBrowser = 1\n",
    "w.ActiveDocument.WebOptions.BrowserLevel = 0 # constants.wdBrowserLevelV4\n",
    "w.ActiveDocument.WebOptions.OrganizeInFolder = 0\n",
    "w.ActiveDocument.WebOptions.UseLongFileNames = 1\n",
    "w.ActiveDocument.WebOptions.RelyOnVML = 0\n",
    "w.ActiveDocument.WebOptions.AllowPNG = 1\n",
    "#w.ActiveDocument.SaveAs( FileName = \"巴啦啦能量\", FileFormat = wc.wdFormatHTML )\n",
    "  \n",
    "# 打印\n",
    "doc.PrintOut()\n",
    "  \n",
    "# 关闭\n",
    "# doc.Close()\n",
    "#w.Documents.Close(wc.wdDoNotSaveChanges)\n",
    "w.Quit()\n",
    "\n",
    "#学习并实验pandas库对Excel文档的处理。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "#学习并实验pandas库对Excel文档的处理。"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.9.7 ('base')",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "2dbe5485cf12282196b80a8ac362415f1d4830d1445e2229f35f94c570ef032e"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
